package com.example.wechatlistview.controls;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ArrayRes;
import androidx.annotation.ColorRes;
import androidx.annotation.StringRes;

import android.content.res.Resources;

import androidx.core.content.ContextCompat;

import com.example.wechatlistview.R;

/**
 * 基于BaseAdapter类的联系人列表适配器
 */
public class ContactsAdapter extends BaseAdapter {
    Context context;
    int[] imageId;
    String[] name;


    public ContactsAdapter(Context c, int[] headId, String[] nameArray) {
        context = c;
        imageId = headId;
        name = nameArray;
    }


    @Override
    public int getCount() {
        return imageId.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        /**
         * 建立view节点设置属性值
         */
        LinearLayout listContainer;
        ImageView headView;
        TextView nameView;

        listContainer = new LinearLayout(context);
        listContainer.setOrientation(LinearLayout.HORIZONTAL);
        listContainer.setPadding(dp2px(12), dp2px(12), dp2px(12), dp2px(12));

        headView = new ImageView(context);
        headView.setLayoutParams(new LinearLayout.LayoutParams(dp2px(45), dp2px(45)));
        headView.setImageResource(imageId[position]);
        headView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        nameView = new TextView(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.weight = 1.0f;
        layoutParams.setMargins(dp2px(8), dp2px(8), dp2px(8), dp2px(8));
        layoutParams.gravity = Gravity.CENTER_VERTICAL;
        nameView.setLayoutParams(layoutParams);
        nameView.setText(name[position]);
        nameView.setTextSize(18);
        nameView.setTextColor(getColorResource(R.color.black));

        listContainer.addView(headView);
        listContainer.addView(nameView);

        return listContainer;
    }

    public String getStrResource(@StringRes int strId) {
        return context.getResources().getString(strId);
    }

    public int getColorResource(@ColorRes int id) {
        return ContextCompat.getColor(context, id);
    }

    public String[] getStrArrayResource(@ArrayRes int id) {
        return context.getResources().getStringArray(id);
    }

    /**
     * 单位转换 dp->px
     *
     * @param dpValue 单位
     * @return px 单位
     */
    public int dp2px(float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
